package cn.com.cennavi.fileMake;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeMap;

import com.vividsolutions.jts.io.ParseException;

import cn.com.cennavi.bean.DifPointTotalMapInfo;

/**
 * 将分车道路况匹配到地图上时，需要指定具体哪条车道，本程序就是生成分车道信息
 * 这一步的输入文件时CloseRoadMapConfigCompile和GroundRoadMapConfigCompile生成的文件
 * 
 * @author Thinkpad
 *
 */
public class LanedataToLaneMap {

	private static String laneMapFile = "D:\\data\\DifPointTotalMapInfo_closeRoad_groundRond_beijing_10mibuf.csv";
	/** 北京分车道地图 **/
	private static String Lanebeijing_mid = "D:\\项目集合\\地图数据\\SJGT201612080001\\SJGT201612080001\\data\\V1.1_newkind\\GreatChina-Addition\\CLM\\CLM\\beijing\\Lanebeijing.mid";
	/** 北京分车道地图 **/
	private static String Lane_Topo_Condbeijing_mid = "D:\\项目集合\\地图数据\\SJGT201612080001\\SJGT201612080001\\data\\V1.1_newkind\\GreatChina-Addition\\CLM\\CLM\\beijing\\Lane_Topobeijing.mid";
	/** 北京r表 **/
	private static String beijing_rtableFile = "D:\\mapinfoData\\beijing_2016\\beijing\\Rbeijing.mid";
	private static String beijing_landata_file = "D:\\data\\beijingLaneMap.csv";
	static HashMap<String, String> map = new HashMap<String, String>();

	public static void main(String[] args) throws IOException, ParseException {
		// TODO Auto-generated method stub
		// 需要做的是：利用进入link退出link得到车道信息
		map.put("1", "001");
		map.put("2", "002");
		map.put("3", "003");
		map.put("4", "004");
		map.put("5", "005");
		map.put("6", "006");
		map.put("7", "007");
		map.put("8", "008");
		map.put("2|1", "009");
		map.put("3|2", "010");
		map.put("4|3", "011");
		map.put("5|4", "012");
		map.put("6|5", "013");
		map.put("7|6", "014");
		map.put("8|7", "015");
		map.put("3|2|1", "016");
		map.put("4|3|2", "017");
		map.put("5|4|3", "018");
		map.put("6|5|4", "019");
		map.put("7|6|5", "020");
		map.put("8|7|6", "021");
		map.put("4|3|2|1", "022");
		map.put("5|4|3|2", "023");
		map.put("6|5|4|3", "024");
		map.put("7|6|5|4", "025");
		map.put("8|7|6|5", "026");
		map.put("5|4|3|2|1", "027");
		map.put("6|5|4|3|2", "028");
		map.put("7|6|5|4|3", "029");
		map.put("8|7|6|5|4", "030");
		map.put("6|5|4|3|2|1", "031");
		map.put("7|6|5|4|3|2", "032");
		map.put("8|7|6|5|4|3", "033");
		map.put("7|6|5|4|3|2|1", "034");
		map.put("8|7|6|5|4|3|2", "035");
		/** key:laneID,value:LaneBean **/
		HashMap<String, LaneBean> LaneBeanMap = getLaneBeanMap(Lanebeijing_mid);

		/** key:laneID,value:该link一共有多少车道数 **/
		HashMap<String, Integer> linkIdLaneNoMap = getLinkIdLaneNoMap(Lanebeijing_mid);

		HashMap<String, ArrayList<LaneTopoBean>> LaneTopoBeanMap = getLaneTopoBeanMap(Lane_Topo_Condbeijing_mid, LaneBeanMap, linkIdLaneNoMap);

		// HashMap<String, Integer> linkLaneNum =
		// getLinkLaneNumber(beijing_rtableFile);

		HashMap<String, DifPointTotalMapInfo> difPointMapInfo = getDifPointMapInfo(laneMapFile, LaneTopoBeanMap, beijing_landata_file);

	}

	/**
	 * 获取每条link的车道数
	 * 
	 * @param lanebeijing_mid2
	 * @return
	 * @throws IOException
	 * @throws NumberFormatException
	 */
	private static HashMap<String, Integer> getLinkIdLaneNoMap(String Lanebeijing_mid) throws NumberFormatException, IOException {
		// TODO Auto-generated method stu
		HashMap<String, Integer> dd = new HashMap<String, Integer>();
		BufferedReader br = new BufferedReader(new FileReader(new File(Lanebeijing_mid)));
		String line = "";
		HashMap<String, TreeMap<Integer, String>> returnList = new HashMap<String, TreeMap<Integer, String>>();
		while ((line = br.readLine()) != null) {
			LaneBean data = LaneBean.parseData(line);
			String linkid = data.LinkID;
			int seqnum = Integer.parseInt(data.Seq_Nm);
			TreeMap<Integer, String> treeMP = returnList.get(linkid);
			if (treeMP != null) {
				treeMP.put(seqnum, "1");
			} else {
				treeMP = new TreeMap<Integer, String>();
				treeMP.put(seqnum, "1");
				returnList.put(linkid, treeMP);
			}
		}
		for (Map.Entry<String, TreeMap<Integer, String>> e : returnList.entrySet()) {
			String linkid = e.getKey();
			TreeMap<Integer, String> v = e.getValue();
			int num = v.lastKey();
			dd.put(linkid, num);
		}
		return dd;
	}

	/**
	 * 获取每条link的数车道数
	 * 
	 * @throws ParseException
	 * @throws IOException
	 **/
	private static HashMap<String, Integer> getLinkLaneNumber(String beijing_rtableFile2) throws IOException, ParseException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(beijing_rtableFile2)));
		String line = "";
		HashMap<String, Integer> dataMap = new HashMap<String, Integer>();
		while ((line = br.readLine()) != null) {
			String[] lineArr = line.split(",");
			String laneNum = lineArr[25].replace("\"", "");
			if (laneNum.equals("")) {
				laneNum = lineArr[26].replace("\"", "");
			}
			Integer laneNums = Integer.parseInt(laneNum);
			String linkId = lineArr[1].replace("\"", "");
			dataMap.put(linkId, laneNums);
		}
		return dataMap;
	}

	private static HashMap<String, DifPointTotalMapInfo> getDifPointMapInfo(String laneMapFile2, HashMap<String, ArrayList<LaneTopoBean>> laneTopoBeanMap, String beijing_landata_file2) throws ParseException, IOException {
		// TODO Auto-generated method stub
		HashMap<String, DifPointTotalMapInfo> returnList = new HashMap<String, DifPointTotalMapInfo>();
		BufferedWriter o = new BufferedWriter(new FileWriter(new File(beijing_landata_file2)));
		BufferedReader br = new BufferedReader(new FileReader(new File(laneMapFile2)));
		String line = "";
		while ((line = br.readLine()) != null) {
			DifPointTotalMapInfo data = DifPointTotalMapInfo.parseData2(line);

			if (data != null) {
				if (data.dir.equals("01") || data.dir.equals("11")) {
					System.out.println(data.enter_out_DifPoint_Link);
				}
				String laneId = data.enter_out_DifPoint_Link.replace("-", "");
//				String[] linkArr = laneId.split("\\|");
				// 四维分车道数据是从左往右，tmc车道数是从右往左，所以需要转换一下
				// int inlinkNum = linkLaneNum.get(linkArr[0]);
				// int outlinkNum = linkLaneNum.get(linkArr[1]);
				// data.inlinkNum = inlinkNum;
				// data.outlinkNum = outlinkNum;
				ArrayList<LaneTopoBean> ss = laneTopoBeanMap.get(laneId);
				TreeMap<Integer, String> intreeMap = new TreeMap<Integer, String>();
				TreeMap<Integer, String> outtreeMap = new TreeMap<Integer, String>();
				if (ss != null) {
					for (LaneTopoBean dt : ss) {
						String inArrow_Dir = dt.inLaneArrow_Dir;
						String outArrow_Dir = dt.outLaneArrow_Dir;
						int inlanno = Integer.parseInt(dt.inLaneIDNo);
						int outlanno = Integer.parseInt(dt.outLaneIDNo);
						intreeMap.put(inlanno, dt.inlinkSeqNo + "|" + inArrow_Dir);
						outtreeMap.put(outlanno, dt.outLinkSeqNo + "|" + outArrow_Dir);
					}
				} else {
					continue;
				}
				StringBuffer sb1 = new StringBuffer();
				StringBuffer sb11 = new StringBuffer();
				for (Map.Entry<Integer, String> e : intreeMap.entrySet()) {
					Integer no = e.getKey();
					String totalNum_tuenflag = e.getValue();
					String[] as = totalNum_tuenflag.split("\\|");
					int totalNum = Integer.parseInt(as[0]);
					String turnFlag = as[1];
					no = totalNum + 1 - no;
					if (no <= 0) {
						// System.out.println("no:"+no+" inlanno:"+e.getKey()+" inlinkNum:"+inlinkNum);
					}
					// System.out.println("no:"+no+" inlanno:"+e.getKey()+" inlinkNum:"+inlinkNum);

					sb1.append(no);
					sb1.append("|");
					sb11.append(turnFlag);
					sb11.append("|");
				}
				// String s1 = sb1.toString();
				String s2 = sb1.toString().substring(0, sb1.length() - 1);
				String s22 = sb11.toString().substring(0, sb11.length() - 1);
				StringBuffer sb2 = new StringBuffer();
				StringBuffer sb22 = new StringBuffer();
				for (Map.Entry<Integer, String> e : outtreeMap.entrySet()) {
					Integer no = e.getKey();
					String totalNum_tuenflag = e.getValue();
					String[] as = totalNum_tuenflag.split("\\|");
					String turnFlag = as[1];
					int totalNum = Integer.parseInt(as[0]);
					no = totalNum + 1 - no;
					if (no <= 0) {
						// System.out.println("no:"+no+" outlanno:"+e.getKey()+" outlinkNum:"+outlinkNum);
					}
					sb2.append(no);
					sb2.append("|");
					sb22.append(turnFlag);
					sb22.append("|");
				}
				// String s11 = sb2.toString();
				String s31 = sb22.toString().substring(0, sb22.length() - 1);
				String s21 = sb2.toString().substring(0, sb2.length() - 1);
				data.inlaneNoSeq = s2;
				data.outlaneNoSeq = s21;
				data.inlaneTurnFlag = s22;
				data.outlaneTurnFlag = s31;
				String[] in_no_turn = intreeMap.lastEntry().getValue().split("\\|");
				data.inlinkNum = Integer.parseInt(in_no_turn[0]);
				String[] out_no_turn = outtreeMap.lastEntry().getValue().split("\\|");
				data.outlinkNum = Integer.parseInt(out_no_turn[0]);

				// 如果车道数==车道总数复制037
				if (data.inlaneNoSeq.split("\\|").length == data.inlinkNum) {
					data.inlaneNoSeq = "037";
				} else {
					String code = map.get(data.inlaneNoSeq);
					if (code != null) {
						data.inlaneNoSeq = code;
					} else {
						data.inlaneNoSeq = "000";
					}
				}
				// 如果车道数==车道总数复制037
				if (data.outlaneNoSeq.split("\\|").length == data.outlinkNum) {
					data.outlaneNoSeq = "037";
				} else {
					String code = map.get(data.outlaneNoSeq);
					if (code != null) {
						data.outlaneNoSeq = code;
					} else {
						data.outlaneNoSeq = "000";
					}
				}
				returnList.put(laneId, data);
				System.out.println(data.toString2());
				o.write(data.toString2());
				o.newLine();
				o.flush();
			}
		}
		return returnList;
	}

	private static HashMap<String, LaneBean> getLaneBeanMap(String Lanebeijing_mid) throws IOException {
		// TODO Auto-generated method stub
		HashMap<String, LaneBean> returnList = new HashMap<String, LaneBean>();
		BufferedReader br = new BufferedReader(new FileReader(new File(Lanebeijing_mid)));
		String line = "";
		while ((line = br.readLine()) != null) {
			LaneBean data = LaneBean.parseData(line);
			String laneId = data.ID;
			returnList.put(laneId, data);
		}
		return returnList;
	}

	/**
	 * 一对inoutlink对应了多对inoutLane
	 * 
	 * @param lane_Topo_Condbeijing_mid2
	 * @param laneBeanMap
	 * @param linkIdLaneNoMap
	 * @return
	 * @throws IOException
	 */
	private static HashMap<String, ArrayList<LaneTopoBean>> getLaneTopoBeanMap(String lane_Topo_Condbeijing_mid2, HashMap<String, LaneBean> laneBeanMap, HashMap<String, Integer> linkIdLaneNoMap) throws IOException {
		// TODO Auto-generated method stub
		HashMap<String, ArrayList<LaneTopoBean>> returnList = new HashMap<String, ArrayList<LaneTopoBean>>();
		BufferedReader br = new BufferedReader(new FileReader(new File(lane_Topo_Condbeijing_mid2)));
		String line = "";
		HashSet<String> inoutLink = new HashSet<String>();
		while ((line = br.readLine()) != null) {
			LaneTopoBean data = LaneTopoBean.parseData(line);
			String inLinkID = data.inLinkID;
			String outLinkID = data.outLinkID;
			int inlinkSeqno = linkIdLaneNoMap.get(inLinkID);
			int outlinkSeqno = linkIdLaneNoMap.get(outLinkID);
			data.inlinkSeqNo = inlinkSeqno;
			data.outLinkSeqNo = outlinkSeqno;
			data.inLaneIDNo = laneBeanMap.get(data.inLaneID).Seq_Nm;
			data.inLaneArrow_Dir = laneBeanMap.get(data.inLaneID).Arrow_Dir;
			data.outLaneArrow_Dir = laneBeanMap.get(data.outLaneID).Arrow_Dir;
			LaneBean ss = laneBeanMap.get(data.outLaneID);
			data.outLaneIDNo = ss.Seq_Nm;
			String inoutL = data.inLinkID + "|" + data.outLinkID;
			if (returnList.containsKey(inoutL)) {
				returnList.get(inoutL).add(data);
			} else {
				ArrayList<LaneTopoBean> datalist = new ArrayList<LaneTopoBean>();
				datalist.add(data);
				returnList.put(inoutL, datalist);
			}
		}
		return returnList;
	}
}
